package com.open.party.cloud.view.examine.fragment

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusRecycleViewBaseFragment
import com.sinothk.android.utils.inters.OnSuperListener
import com.open.party.cloud.R
import com.open.party.cloud.model.*
import com.open.party.cloud.view.examine.ExamineAttendanceHistoryActivity
import com.open.party.cloud.view.examine.adapter.ExamineClassesAttendanceListForTodayAdapter
import com.open.party.cloud.viewModel.ExamineViewModel
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_fragment_rv_loading_list.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


open class ExamineClassesAttendanceListForAllFragment : AppEventBusRecycleViewBaseFragment<CommVo>(), OnSuperListener<AttendanceEntity> {

    var viewModel: ExamineViewModel? = null
    var myActivity: ExamineAttendanceHistoryActivity? = null
    private var adapter: ExamineClassesAttendanceListForTodayAdapter? = null
    private var currView: View? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        myActivity = activity as ExamineAttendanceHistoryActivity?
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.comm_fragment_rv_loading_list, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ExamineViewModel()

        initRecycleLinearView(loadingRecyclerView)

        adapter = ExamineClassesAttendanceListForTodayAdapter(activity)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("努力加载中")
            refreshData()
        }

        loadingTipView.showLoading("努力加载中")
        refreshData()
    }

    override fun loadData(pageVo: PageVo<CommVo>) {
        val pageReq = PageReq<ExamineClassVO>()
        pageReq.pageNo = pageVo.pageNum
        pageReq.pageSize = pageVo.pageSize

        val vo = ExamineClassVO()
        vo.classId = myActivity!!.classId!!
        pageReq.data = vo

        viewModel!!.getExamineClassesAttendanceListForAll(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<AttendanceEntity>>) {
        if ("getExamineClassesAttendanceListForAll" != result.eventType) {
            return
        }

        stopLoading(loadingRecyclerView, loadType)

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            40001 -> {
                loadingTipView.showError("请重新登录后再使用")
            }
            40013 -> {
                loadingTipView.showError("请重新登录后再使用")
            }
            40011 -> {
                loadingTipView.showError("请重新登录后再使用")
            }

            40015 -> {
                loadingTipView.showError("请重新登录后再使用")
//                XUtils.intent().openActivity(activity, LoginActivity::class.java).start()
            }

            else -> {
                loadingTipView.showError(result.msg)
            }
        }
    }

    private fun setPageData(appPageData: AppPageData<AttendanceEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
                loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                loadingRecyclerView.setLoadingMoreEnabled(false)
            } else {
                adapter!!.setData(appPageData.records!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }
    }

    override fun onClick(position: Int, itemData: AttendanceEntity, flag: String?) {
//        if (itemData != null) {
//            XUtils.intent().openActivity(activity, ExamineClassExamineListActivity::class.java)
//                .putSerializableExtra("examineClass", itemData)
//                .startInFragment(this)
//        }
    }
}